package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-02-25
 * Time: 19:51
 */
class MyArray<E>{
    public Object[] array = new Object[10];

    public void setValue(int pos,E val) {
        array[pos] = val;
    }

    public E getValue(int pos) {
        return (E)array[pos];
    }
}
public class demo4 {
    public static void main(String[] args) {
        //裸泛型
        MyArray myArray = new MyArray();
        myArray.setValue(0,10);
        myArray.setValue(1,"s");
        int a = (int)myArray.getValue(0);
        String s = (String)myArray.getValue(1);
        System.out.println(a + s);
    }
    public static void main1(String[] args) {
//        对demo3遗留的两个问题：使用泛型进行解决
        MyArray<Integer> myarray1 = new MyArray<Integer>();
        myarray1.setValue(0,10);
        //自动类型检查
//        myarray1.setValue(1,"hello");
        myarray1.setValue(1,20);

        MyArray<String> myarray2 = new MyArray<String>();
        myarray2.setValue(0,"hello ");
        myarray2.setValue(1,"world");

        //自动类型转换
        int a = myarray1.getValue(0);
        System.out.println(a);

        String str1 = myarray2.getValue(0);
        System.out.println(str1 + myarray2.getValue(1));
    }
}
